// pages/evaluate/evaluate.js
const app = getApp();

Page({
  data: {
    oid   : 0,
    bid   : 0,
    // first : '非常不满意',
    first: '',
    star  : 0,
    create_time : '',
    business    : {},
    goods       : [],
    userStars: [
      { url: '/img/selected.png',},
      { url: '/img/normal.png',},
      { url: '/img/normal.png',},
      { url: '/img/normal.png',},
      { url: '/img/normal.png',},
    ],
    txt: [
      '非常不满意',
      '不满意',
      '一般',
      '满意',
      '非常满意，无可挑剔'
    ],
    server:false,
    quality:false,
    send:false,
    un_server: false,
    un_quality: false,
    un_send: false,
    taidu:'',
    zhiliang: '',
    fahuo: '',
  },
  starTap: function (e) {
    var index = e.currentTarget.dataset.index; // 获取当前点击的是第几颗星星
    var tempUserStars = this.data.userStars; // 暂存星星数组
    var len = tempUserStars.length; // 获取星星数组的长度
    for (var i = 0; i < len; i++) {
      if (i <= index) { // 小于等于index的是满心
        tempUserStars[i].url = '/img/selected.png'
      }else{ // 其他是空心
        tempUserStars[i].url = '/img/normal.png'
      }
    }
    var txt = this.data.txt;
    var first;
    var star = this.data.star;
    for (var a = 0; a < txt.length; a++) {
      if (a == index) { // 小于等于index的是满心
        first = txt[a],
        star  = a+1
      }
    }
    // 重新赋值就可以显示了
    this.setData({
      userStars : tempUserStars,
      first     : first,
      star      : star
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var oid  = options.oid;
    var that = this;
    wx.request({
      url     : app.url + '/Order/evaluation',
      method  : 'post',
      data    : { oid: oid },
      success : function (res) {
        that.setData({
          oid         : res.data.data.oid,
          bid         : res.data.data.bid,
          create_time : res.data.data.create_time,
          business    : res.data.data.business,
          goods       : res.data.data.goods
        })
      },
      error: function () { }
    })
  },
  formSubmit : function(e){
    var that = this
    var bid = e.detail.target.dataset.bid;
    var oid = e.detail.target.id;
    var content = e.detail.value.content;
    var uid = wx.getStorageSync('uid');
    // var star    = e.detail.target.dataset.star;
    // var level   = e.detail.target.dataset.level;
    console.log(that.data.server)
    console.log(that.data.un_server)
    console.log(that.data.quality)
    console.log(that.data.un_quality)
    console.log(that.data.send)
    console.log(that.data.un_send)
    if (!that.data.server) { var taidu = 1; that.setData({ taidu: taidu })} 
    else if (!that.data.un_server) { taidu = 0; that.setData({ taidu: taidu })}
    if (!that.data.quality) { var zhiliang = 1; that.setData({ zhiliang: zhiliang })} 
    else if (!that.data.un_quality) { var zhiliang = 0; that.setData({ zhiliang: zhiliang })}
    if (!that.data.send) { var fahuo = 1;that.setData({ fahuo: fahuo })} 
    else if (!that.data.un_send) { var fahuo = 1;that.setData({ fahuo: fahuo })}
    console.log(that.data.taidu)
    console.log(that.data.zhiliang)
    console.log(that.data.fahuo)
    if (!taidu || !zhiliang || !fahuo){
      wx.showToast({
        title: '请选择完评论再提交',
      },1000)
      return false
    }
    wx.request({
      url      : app.url + '/Order/evaluation_add',
      method   : 'post',
      // data     : {uid:uid, bid:bid, oid:oid, star:star,level:level, content:content},
      data: { uid: uid, bid: bid, oid: oid, taidu: that.data.taidu, zhiliang: that.data.zhiliang, fahuo: that.data.fahuo },
      success  : function(res){
        if(res.data.code == 1){
          wx.showToast({ title: '评价成功',duration: 1500 })
          setTimeout(function (){ wx.redirectTo({ url: '../order/order' }) }, 1500)
        }else{
          wx.showToast({ title: '评价失败'})
        }
      },
      error: function () { wx.showToast({ title: '请求错误', })}
    })
  },
  server_handler:function(e){
    console.log(e.currentTarget.dataset.status)
    var server_status = e.currentTarget.dataset.status;
    if (server_status=="true") {
      this.setData({ server: false, un_server: true })
    } else {
      this.setData({ server: true, un_server: false })
    }
  },
  quality_handler:function(e){
    console.log(e.currentTarget.dataset.status)
    var quality_status = e.currentTarget.dataset.status;
    if (quality_status == "true") {
      this.setData({ quality: false, un_quality: true })
    } else {
      console.log("unq");
      this.setData({ quality: true, un_quality: false })
    }
  },
  send_handler:function(e){
    console.log(e.currentTarget.dataset.status)
    var send_status = e.currentTarget.dataset.status;
    if (send_status == "true") {
      this.setData({ send: false, un_send: true })
    } else {
      this.setData({ send: true, un_send: false })
    }
  },
  onShow: function () {

  },
  onReady: function () {
  
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
  
  }
})